<?php
class PostsController extends Zendvn_Controller_Action
{
	public function init()
	{
		$template_path=TEMPLATE_PATH."/public/system";
		$this->loadTemplate($template_path);
		$this->view->headTitle()->append("-View Posts"); //Hien thi headTitle()-San pham tren cung cua trinh duyet web
		//$this->view->controller = $this->_request->getParam('controller'); //Lay gia tri Controller hien thi tren link va luu no vao tham bien 'controller' giong nhu getParam('Id')
	}																	//Gia tri 'controller' nay duoc truyen sang header.php de xet dieu kien if(controller=='default') hien thi cac helpers
	public function indexAction()
	{
		// action body
	}

	public function viewAction() //View comment & add comment
	{
		// action body (Khoi lenh nay view noi dung cua bai viet duoc get $postid)
		
		$postid = (int)$this->_getParam('id'); //Lay id cua bai Post $postid la 1 so int
		if( empty($postid) ) {}
		else{
			$post = new Default_Model_DbTable_Posts(); //Tao doi tuong trong Model Posts
			//function getPost()chi get ra 1 bai post co id ='id'
			$result = $post->getPost($postid); //Su dung method gePost($id_post) trong class Model_DbTable_Posts tra gia tri mang bao gom thong tin bai Post
			
			//Count 'luotxem' bai post
			$views= $result['luotxem'];
			$post->countViewPost($postid,$views);
			
			$this->view->post = $result; //Hien thi view ra noi dung cua bai post
		}
		
		$commentsObj = new Default_Model_DbTable_Comments(); //Tao doi tuong class Model Comment
		$commentsForm = new Default_Form_Comments();
		$request = $this->getRequest(); // de thuc hien chuan hoa form o dong 45 phia duoi $this->getRequest()->getPost()
		/*
		 * Check the comment form has been posted
		 */
		
		//Khoi lenh thuc thi Them Insert vao mot comment moi thong qua phuong thuc saveComment()
		
		if ($this->getRequest()->isPost()) { //Neu comment form da duoc posted tuc la da submited = true
			if ($commentsForm->isValid($request->getPost())) { //Kiem tra Valid cho du lieu nhap vao form
				$model = new Default_Model_DbTable_Comments();
				//Dung $commentsForm->getValues() thay vi dung $this->getRequest()->getPost('column_name')boi vi phuong thuc saveComment()bien truyen vao la mot mang cac cot(ko can lay ra gia tri cua tung column)
				$model->saveComment($commentsForm->getValues());//su dung getValues() lay gia tri comment tu form va phuong thuc saveComment() trong Model Comments de saved comment nay
				$commentsForm->reset();//Insert xong thuc hien reset() cac thanh phan form comment ve NULL
			}
		}
		//$data = array( 'id'=> $postid );
		//$commentsForm->populate( $data ); //Giu lai gia tri tren Form de nguoi dung sua neu submited khong thanh cong.
		
		$this->view->commentsForm = $commentsForm; //Show view form comments
		
		//getComments($postid) su dung fetchall()vi mot bai post co id='id' thi co nhieu comments.
		$comments = $commentsObj->getComments($postid); //su dung phuong thuc getComments() trong Model Comments de lay gia tri $id cua bai comment
		$this->view->comments = $comments; //Show View ra noi dung cua cac comments cho bai post nay .Moi tham bien comments se chua id comment duoc render.Khi sang view.pthml ta foreach comments de hien thi tat ca cac comments
		
		//Render tham bien edit cung string chua link cua no sang view.phtml de goi trog < a href="link" /> Edit </a>
		$this->view->edit = '/posts/edit/id/'.$postid;
		
		//Render tham bien delete cung string chua link cua no sang view.phtml de goi trog < a href="link" /> Delete </a>
		$this->view->delete = '/posts/delete/id/'.$postid;
		
	}

	public function commentsAction()
	{
		// action body
	}
	
	public function addAction() //Add post
	{
		// action body
		if(!Zend_Auth::getInstance()->hasIdentity()) { //Khoi tao doi tuong Zend_Auth kiem tra chung thuc neu == true thi...
			echo '<h1>U cant Add post because U dont login</h1>';
			//$this->_redirect('index/index');
		}
        
		$acl = new Default_Model_Acl();
		$identity = Zend_Auth::getInstance()->getIdentity(); //La doi tuong array chua thong tin getIdentity tu doi tuong khoi tao Instance cua Auth(cu the o day chinh la mang row chua thong tin cac column trong table Users (chua Auth nhu Admin,blogger...))
		
       	if( $acl->isAllowed( $identity['Role'] ,'posts','add') ) { //Method kiem tra quyen isAllowed() xem neu $identity duoc getDdentity() colum 'Role' trong table users co parivilate ('add','post') la true thi...
        	$request = $this->getRequest(); //Dung de isValid(getPost()) nhu dong code71 phia duoi
        	$postForm = new Default_Form_Post();
        	//Khoi lenh thuc hien Them insert bai Post thong qua method savePost()
        	if ($this->getRequest()->isPost()) {
        		if ($postForm->isValid($request->getPost())) {
                	$model = new Default_Model_DbTable_Posts();
                	//Xem chu giai method saveComments() o tren de hieu duoc method savePost()
                	$model->savePost($postForm->getValues()); //su dung getValues() lay cac gia tri post tu form va phuong thuc saveComment() trong Model Posts de saved Post nay
                	$this->_redirect('index/index'); //Save xong noi dung add Post thi redirect ve trang Index.
               	}
          	}
          	//Cau lenh duoi phai nam trong if()vi neu nhu da duoc chung thuc la dang nhap roi nhung ko co quyen 'add' 'post' thi ko hien thi postform
           	$this->view->postForm = $postForm; //Render form Add Post den view add.pthml
    	}
    	else  //Hien thi thong bao neu role khong duoc quyen Add Post
    		echo '<h1>U cant add post because your role cant Add </h1>';
	}
	
	public function editAction() //Edit post
	{
		// action body
		
		$postid = (int)$this->_getParam('id'); //Lay id cua bai Post $posid la 1 so int
		$request = $this->getRequest();
		
		if(!Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('posts/view/id/'.$postid);
		}
		
		$identity = Zend_Auth::getInstance()->getIdentity(); //getInstance Khoi tao mot doi tuong Zend_Auth va getIdentity dung de lay thong tin ve bien khoi tao da duoc chung thuc hasIdentity truoc do (vi du tu cookie chang han)
		$acl = new Default_Model_Acl();
		
		if( $acl->isAllowed( $identity['Role'] ,'posts','edit') ) {
			$postForm = new Default_Form_Post();
			$postModel = new Default_Model_DbTable_Posts();
			//Khoi lenh thuc hien Edit Post thong qua Method updatePost()
			if ($this->getRequest()->isPost()) {
				if ($postForm->isValid($request->getPost())) {
					$postModel->updatePost($postForm->getValues()); //su dung getValues() lay gia tri bai Post tu form va phuong thuc UpdatePost() trong Model Posts de Edit post nay
					$this->_redirect('posts/view/id/'.$postid); //Sau khi Edit xong thi redirect den trang view noi dung bai Post vua Edit
				}
			} else { // Hai cau lenh duoi day get mang cac gia tri tu CSDL va populate no len form de Edit
				$result = $postModel->getPost($postid);//doi tuong $postModel get gia tri columns trong CSDL gan no vao $result de dong duoi lenh duoi get $result len form
				$postForm->populate( $result ); //Nguoc lai Neu gia tri nhap vao Form khong dung theo chuan thi giu lai gia tri nay tren Form de nguoi dung co the sua lai.Chu khong xoa toan bo cac thuoc tinh tren Form thanh NULL
			}
			$this->view->postForm = $postForm; //Render  Form_Post (Form nay cung dung voi truong hop Add post)
		} else { //Nguoc lai neu vai tro khong dung nhu cau lenh if () o tren thi thuc hien khoi lenh duoi.Nhung khoi lenh else nay la khong can thiet(vi trong view.phtml chi hien thi Button Edit voi tai khoan co Role hop le)
			var_dump( $identity['Role'] );
			$this->_redirect('posts/view/id/'.$postid);
		}
	}
	
	public function deleteAction()
	{
		// action body
		
		$postid = (int)$this->_getParam('id'); //Lay id cua bai Post $posid la 1 so int
		$request = $this->getRequest();
		
		if(!Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('posts/delete/id/'.$postid);
		}
		
		$identity = Zend_Auth::getInstance()->getIdentity(); //getInstance Khoi tao mot doi tuong Zend_Auth va getIdentity dung de lay thong tin ve bien khoi tao da duoc chung thuc hasIdentity truoc do (vi du tu cookie chang han)
		
		$acl = new Default_Model_Acl();
		
		if( $acl->isAllowed( $identity['Role'] ,'posts','delete') ) {
			if  ($this->getRequest()->isPost())  {
				$del = $this->getRequest()->getPost('del');   
				if ($del == 'Yes') {
					$id  =  $this->getRequest()->getPost('id'); 
					$temp = new Default_Model_DbTable_Posts(); 
					$temp->deletePost($id);
				}	

				$this->_redirect('/index/index'); //Xoa xong thi quay tro ve trang index
			
			} 
			// $postid = (int)$this->_getParam('id'); //Lay id cua bai Post $posid la 1 so int
			$post = new Default_Model_DbTable_Posts(); //Tao doi tuong trong Model Posts
			$result = $post->getPost($postid); //Su dung method gePost($id_post) trong class Model_DbTable_Posts tra gia tri mang bao gom thong tin bai Post
			$this->view->deletes = $result; //Hien thi view ra noi dung cua bai post
		}
	}
}